{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2023-10-25 16:05:58--  https://huggingface.co/datasets/mesolitica/chatgpt-explain-sentiment/resolve/main/explain-sentiment.json\n",
      "Resolving huggingface.co (huggingface.co)... 18.67.181.124, 18.67.181.100, 18.67.181.126, ...\n",
      "Connecting to huggingface.co (huggingface.co)|18.67.181.124|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://cdn-lfs.huggingface.co/repos/9b/7a/9b7a8222bff178eab23966edcdd936e76ad1388751f2fb906a99650a7eb920dc/55700946bedffa48b7c065d4348fe9abea94ee34a05c6e6ca9e12f18b1c78ede?response-content-disposition=attachment%3B+filename*%3DUTF-8%27%27explain-sentiment.json%3B+filename%3D%22explain-sentiment.json%22%3B&response-content-type=application%2Fjson&Expires=1698480365&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTY5ODQ4MDM2NX19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5odWdnaW5nZmFjZS5jby9yZXBvcy85Yi83YS85YjdhODIyMmJmZjE3OGVhYjIzOTY2ZWRjZGQ5MzZlNzZhZDEzODg3NTFmMmZiOTA2YTk5NjUwYTdlYjkyMGRjLzU1NzAwOTQ2YmVkZmZhNDhiN2MwNjVkNDM0OGZlOWFiZWE5NGVlMzRhMDVjNmU2Y2E5ZTEyZjE4YjFjNzhlZGU%7EcmVzcG9uc2UtY29udGVudC1kaXNwb3NpdGlvbj0qJnJlc3BvbnNlLWNvbnRlbnQtdHlwZT0qIn1dfQ__&Signature=DLB6W5%7Ej5JbvINRT2-HUAcC36TI9lAsuOicVdBornQ89d6SHfKdmlmmIcpH4Fg7psEXRN-hTEJVbNmV0QjIKWiSjfX2b8J0nYi%7EBd6pM-XBRyV9sterweBXEvInxCxGv%7EVTUJcfRDBo3Uv46bWfGK53ctQMCM9DwaS5-qCu%7EqUntrp2A7UpPHApiSX24JVYKEvqNobFUyq0Gj-Odrq6ibdeAZZnRfIsWTe5CeSvDuvOB4S1zVZWC%7E7wTxKeJn%7ED7EQIN7dep1ET0znx90PQ74O3L7ISO5hK0tkiuomMKcFSymYEkdEGkb6COd99Uw3ytrOdHCA6%7E0T%7EuKYByQqv62Q__&Key-Pair-Id=KVTP0A1DKRTAX [following]\n",
      "--2023-10-25 16:05:59--  https://cdn-lfs.huggingface.co/repos/9b/7a/9b7a8222bff178eab23966edcdd936e76ad1388751f2fb906a99650a7eb920dc/55700946bedffa48b7c065d4348fe9abea94ee34a05c6e6ca9e12f18b1c78ede?response-content-disposition=attachment%3B+filename*%3DUTF-8%27%27explain-sentiment.json%3B+filename%3D%22explain-sentiment.json%22%3B&response-content-type=application%2Fjson&Expires=1698480365&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTY5ODQ4MDM2NX19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy5odWdnaW5nZmFjZS5jby9yZXBvcy85Yi83YS85YjdhODIyMmJmZjE3OGVhYjIzOTY2ZWRjZGQ5MzZlNzZhZDEzODg3NTFmMmZiOTA2YTk5NjUwYTdlYjkyMGRjLzU1NzAwOTQ2YmVkZmZhNDhiN2MwNjVkNDM0OGZlOWFiZWE5NGVlMzRhMDVjNmU2Y2E5ZTEyZjE4YjFjNzhlZGU%7EcmVzcG9uc2UtY29udGVudC1kaXNwb3NpdGlvbj0qJnJlc3BvbnNlLWNvbnRlbnQtdHlwZT0qIn1dfQ__&Signature=DLB6W5%7Ej5JbvINRT2-HUAcC36TI9lAsuOicVdBornQ89d6SHfKdmlmmIcpH4Fg7psEXRN-hTEJVbNmV0QjIKWiSjfX2b8J0nYi%7EBd6pM-XBRyV9sterweBXEvInxCxGv%7EVTUJcfRDBo3Uv46bWfGK53ctQMCM9DwaS5-qCu%7EqUntrp2A7UpPHApiSX24JVYKEvqNobFUyq0Gj-Odrq6ibdeAZZnRfIsWTe5CeSvDuvOB4S1zVZWC%7E7wTxKeJn%7ED7EQIN7dep1ET0znx90PQ74O3L7ISO5hK0tkiuomMKcFSymYEkdEGkb6COd99Uw3ytrOdHCA6%7E0T%7EuKYByQqv62Q__&Key-Pair-Id=KVTP0A1DKRTAX\n",
      "Resolving cdn-lfs.huggingface.co (cdn-lfs.huggingface.co)... 18.161.180.7, 18.161.180.53, 18.161.180.15, ...\n",
      "Connecting to cdn-lfs.huggingface.co (cdn-lfs.huggingface.co)|18.161.180.7|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 94061860 (90M) [application/json]\n",
      "Saving to: ‘explain-sentiment.json’\n",
      "\n",
      "explain-sentiment.j 100%[===================>]  89.70M  11.0MB/s    in 9.4s    \n",
      "\n",
      "2023-10-25 16:06:09 (9.51 MB/s) - ‘explain-sentiment.json’ saved [94061860/94061860]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://huggingface.co/datasets/mesolitica/chatgpt-explain-sentiment/resolve/main/explain-sentiment.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "96791"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('explain-sentiment.json') as fopen:\n",
    "    data = json.load(fopen)\n",
    "    \n",
    "texts, labels = [], []\n",
    "\n",
    "labels_sentiment = ['negative', 'positive']\n",
    "for d in data:\n",
    "    try:\n",
    "        l = json.loads(d[1])\n",
    "        if not isinstance(l['sentiment'], str):\n",
    "            continue\n",
    "        if l['sentiment'] not in labels_sentiment:\n",
    "            continue\n",
    "        if not len(d[0]):\n",
    "            continue\n",
    "        texts.append(d[0])\n",
    "        labels.append(labels_sentiment.index(l['sentiment']))\n",
    "    except:\n",
    "        pass\n",
    "    \n",
    "len(texts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Terimakasih ya', 1)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "texts[0], labels[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_labels = ['sentimen negatif', 'sentimen positif']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "X1, X2, Y = [], [], []\n",
    "\n",
    "for i in range(len(labels)):\n",
    "    if random.random() > 0.5:\n",
    "        Y.append(0)\n",
    "        X2.append(new_labels[int(not labels[i])])\n",
    "    else:\n",
    "        Y.append(1)\n",
    "        X2.append(new_labels[labels[i]])\n",
    "    X1.append(texts[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(96791, 96791, 96791)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(X1), len(X2), len(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Terimakasih ya sentimen positif 1\n",
      "Karya tangan anak bangsa sekarang patut diacungi jempol apalagi produknya yang yang bagus, banggalah kita memakai k sentimen negatif 0\n",
      "BABUN PUNYA AMZAR. TAK RETI HORMAT ORANG KEEEEEE???!!!! sentimen negatif 1\n",
      "kau tak payah bagi data ni pukimak!! wahai pu sentimen negatif 1\n",
      "Dah tak tahu nak buat apa dengan info ni. sentimen negatif 1\n",
      "hati hati ya semua. Kami .I anti main sentimen politik buka aib. Jual maruah manusia....I alert semua sentimen negatif 1\n",
      "Yeahh tukar kerajaan confirm kilang tutup sebulan... sentimen positif 0\n",
      "Siapa yg pilih manusia fake ni ? Kaki drama, serik dah aku kena kencing zaman dia jadi mente sentimen negatif 1\n",
      "Alhamdulillah baby Umar dh selamat sampai Kajang sentimen negatif 0\n",
      "Org tua ni pembangkang dlm Kerajaan ka apa ? Dia punya bodo till jannah sentimen negatif 1\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    print(X1[i], X2[i], Y[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('sentiment.json', 'w') as fopen:\n",
    "    json.dump({\n",
    "        'X1': X1,\n",
    "        'X2': X2,\n",
    "        'Y': Y,\n",
    "    }, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
